import { createRouter, createWebHistory } from 'vue-router'
import { useTabsStore } from '../store/store.js';
import { getToken } from '../utils/auth.js'

import Login from '../view/login.vue'
import Home from '../view/home.vue'
import Register from '../view/register.vue'
import HomePage from '../components/index.vue'
import Emty from '../components/empty.vue'

import Authority from '../components/Authority.vue'
import DataAnalysis from '../components/DataAnalysis.vue'
import DataExport from '../components/DataExport.vue'
import DepartmentList from '../components/DepartmentList.vue'
import FaultAndAnalysis from '../components/FaultAndAnalysis.vue'
import Message from '../components/Message.vue'
import PersonList from '../components/PersonList.vue'
import UserList from '../components/UserList.vue'
import WorkOrderHistory from '../components/WorkOrderHistory.vue'
import WorkOrderList from '../components/WorkOrderList.vue'
import ReplyMessage from '../components/ReplyMessage.vue'
import MessageReviewed from '../components/MessageReviewed.vue'
import OvertimeWorkOrder from '../components/OvertimeWorkOrder.vue'
import GroupWorkOrder from '../components/GroupWorkOrder.vue'
import MessageDetail from '../components/MessageDetail.vue'
import Setting from '../components/Setting.vue'

const routes = [
    { path: '/', redirect: 'login' },
    { path: '/login', name: 'login', component: Login },
    { path: '/register', name: 'register', component: Register },
    {
        path: '/home', name: 'home', meta: { title: '首页' }, redirect: 'homePage', component: Home, children: [
            {
                path: '/homePage', component: HomePage, meta: { title: '首页' }
            },
            {
                path: '/UserList', meta: { title: '用户管理' }, component: Emty, redirect: 'UserList', children: [
                    { path: '/UserList', component: UserList, meta: { title: '用户列表' } },
                    { path: '/Authority', component: Authority, meta: { title: '权限分配' } },
                ]
            },
            {
                path: '/DepartmentList', meta: { title: '部门列表' }, component: Emty, redirect: 'DepartmentList', children: [
                    { path: '/DepartmentList', component: DepartmentList, meta: { title: '部门列表' } },
                    { path: '/PersonList', component: PersonList, meta: { title: '人员列表' } },
                    { path: '/Message', component: Message, meta: { title: '消息通知' } }
                ]
            },
            {
                path: '/WorkOrderList', meta: { title: '工单管理' }, component: Emty, redirect: 'WorkOrderList', children: [
                    { path: '/WorkOrderList', component: WorkOrderList, meta: { title: '工单列表' } },
                    { path: '/WorkOrderHistory', component: WorkOrderHistory, meta: { title: '工单审批' } },
                    { path: '/FaultAndAnalysis', component: FaultAndAnalysis, meta: { title: '故障分析' } }
                ]
            },
            {
                path: '/DataAnalysis', meta: { title: '数据分析' }, component: Emty, redirect: 'DataAnalysis', children: [
                    { path: '/DataAnalysis', component: DataAnalysis, meta: { title: "数据分析" } },
                    { path: '/DataExport', component: DataExport, meta: { title: "导入导出" } }
                ]
            },
            { path: '/ReplyMessage', name: 'ReplyMessage', component: ReplyMessage, meta: { title: '待回复消息' } },
            { path: '/MessageReviewed', name: 'MessageReviewed', component: MessageReviewed, meta: { title: '待审核消息' } },
            { path: '/OvertimeWorkOrder', name: 'OvertimeWorkOrder', component: OvertimeWorkOrder, meta: { title: '超时工单' } },
            { path: '/GroupWorkOrder', name: 'GroupWorkOrder', component: GroupWorkOrder, meta: { title: '所在组工单' } },
            { path: '/MessageDetail', name: 'MessageDetail', component: MessageDetail, meta: { title: '公告详情' } },
            { path: '/Setting', name: 'Setting', component: Setting, meta: { title: '个人设置' } }
        ]
    },
]

const router = new createRouter({
    history: createWebHistory(),
    routes
})

// 在路由守卫中
router.beforeEach((to, from, next) =>
{
    const tabsStore = useTabsStore();
    tabsStore.initializeFromLocalStorage();
    // 检查是否是首页路由
    const isHomeRoute = to.path === '/home'; // 根据你的首页路由路径来定义条件

    // 检查是否是登录页或注册页面
    const isLoginRoute = to.path === '/login';
    const isRegisterRoute = to.path === '/register';
    // 检查用户是否拥有有效的 token
    const hasToken = getToken() // 根据具体的逻辑从存储或其他方式获取用户的 token 状态;
    console.log(hasToken, "拿取到的token");
    if (!isHomeRoute && !isLoginRoute && !isRegisterRoute) {
        // 检查标签是否已存在
        const tabExists = tabsStore.tabs.some(tab => tab.path === to.path);
        // 增加活动状态activeTab
        tabsStore.addActiveTab(to.path);
        if (!tabExists) {
            tabsStore.addTab({
                path: to.path,
                meta: to.meta,
                closable: true
            });
        }
    }

    // 根据用户 token 的状态决定是否跳转到登录页面
    // 如果有有效的 token，正常跳转
    // 否则跳转到登录页面
    next();
});




export default router;